/*
 * chapter_11.cpp
 *
 *	关联容器
 *  Created on: 2020年11月25日
 *      Author: san
 */

#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <functional>
#include <utility>
#include <string>
#include <fstream>
#include <sstream>

#include "Sales_data.h"

using std::string;
using std::ifstream;

void t11_3()
{
	std::map<string, string::size_type> word_count;
	string word;
	while (std::cin >> word and word != "0")
		++word_count[word];

	for (const auto &w: word_count)
	{
		std::cout << w.first << " occurs " << w.second
				<< ((w.second > 1) ? " times" : " time")  << std::endl;
	}
}

void tool_11_4(string &s)
{
	/*
	 * 这里将题目做了简化
	 * 使用remove_if, 和 ispunct
	 */
	string alphbet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	string::size_type pos = 0;
	while ((pos = s.find_first_not_of(alphbet, pos)) != string::npos)
	{
		s.erase(pos, 1);
	}

	for (auto &c: s)
		c = tolower(c);
}

void t11_4()
{
	std::map<string, string::size_type> word_count;
	string word;
	while (std::cin >> word && word != "0")
	{
		tool_11_4(word);
		++word_count[word];
	}

	for (const auto &w: word_count)
	{
		std::cout << w.first << " occurs " << w.second
				<< ((w.second > 1) ? " times" : " time")  << std::endl;
	}
}

bool compareInfo(const Sales_data &sd1, const Sales_data &sd2)
{
	return sd1.isbn() > sd2.isbn();
}


void t11_8()
{
	/*
	 * 使用vector实现set功能
	 */

	string word;
	std::vector<string> svec;

	while (std::cin >> word && "0" != word)
	{
		if (std::find(svec.begin(), svec.end(), word) == svec.end())
			svec.push_back(word);
	}

	for (auto w: svec)
	{
		std::cout << w << " ";
	}
}

int t11_11()
{
	bool (*pf)(const Sales_data &sd1, const Sales_data &sd2) = compareInfo;
	using compare = bool (*)(const Sales_data &sd1, const Sales_data &sd2);
	std::multiset<Sales_data, decltype(compareInfo)*> bookStore(compareInfo);
	std::multiset<Sales_data, compare> book_store(compare);
	std::multiset<Sales_data, bool (*)(const Sales_data &sd1, const Sales_data &sd2)> book_store1(compare);
}

void t11_12()
{
	std::vector<string> svec{"one", "two", "three", "four", "five"};
	std::vector<int> ivec{1, 2, 3, 4, 5};
	std::vector<string>::iterator ster = svec.begin();
	std::vector<int>::iterator iter = ivec.begin();

	std::vector<std::pair<int, string>> result;

	while (ster != svec.end() && iter != ivec.end())
	{
		result.push_back({*iter, *ster});
		++ster;
		++iter;
	}

	for (auto x: result)
	{
		std::cout << x.first << " " << x.second << std::endl;
	}
}

void t11_20()
{
	string word;
	std::map<string, string::size_type> word_count;

	while (std::cin >> word && word != "0")
	{
		auto ret = word_count.insert({word,  1});
		if (!ret.second)
			++ret.first->second;
	}

	for (auto w: word_count)
	{
		std::cout << w.first << " " << w.second << " ";
	}
	std::cout << std::endl;
}

void t11_22()
{
	/*
	 * 插入一个元素的insert版本
	 */
	std::map<string, std::vector<int>> ismap;
	ismap.insert(ismap.begin() ,{"one", std::vector<int>{1}});

	std::for_each(ismap.begin(), ismap.end(),
			[](const std::pair<string, std::vector<int> > &tmap){std::cout << tmap.first;});
}

void t11_31()
{
	std::multimap<string, string> authors{{"zhangsan", "woshizhangsan"}, {"lisi", "woshilisi"}};
	std::multimap<string, string>::iterator fb;
	while ((fb = authors.find("zhangsan")) != authors.end())
	{
		authors.erase(fb);
	}

	std::for_each(authors.begin(), authors.end(),
			[](const std::pair<string ,string> &p){std::cout << p.second;});
}

void buildMap(ifstream &map_file, std::map<string, string> &mmap)
{
	string line, word;
	while (getline(map_file, line))
	{
		std::istringstream mstream(line);
		string first, second;
		mstream >> first >> second;  // 这里还可以改进
		mmap.insert({first, second});
	}
}

string transform(const string &s, std::map<string, string> &mmap)
{
	auto ret = mmap.find(s);
	if (ret != mmap.end())
	{
		return ret->second;
	}
	else
		return s;
}

void word_transform(ifstream &map_file, ifstream &input)
{
	std::map<string, string> mmap;
	string line, word;
	buildMap(map_file, mmap);
	while (getline(input, line))
	{
		std::istringstream lstream(line);
		while (lstream >> word)
		{
			std::cout << transform(word, mmap) << " ";
		}
		std::cout << std::endl;
	}
}

void t11_33()
{
	ifstream map_file("/home/san/eclipse-workspace/CppPrimer/file/map_file.txt"), input("/home/san/eclipse-workspace/CppPrimer/file/input.txt");
	word_transform(map_file, input);
}

void t11_38()
{
	std::unordered_map<string, int> ssmap;
	ssmap.insert({"one", 1});
}

int main11()
{

	std::cout << "-----------------使用关联容器--------------------------" << std::endl;
//	t11_3();
//	t11_4();
//	t11_8();

	std::cout << "-----------------pair--------------------------" << std::endl;
	t11_12();

	std::cout << "------------------添加元素------------------------" << std::endl;
//	t11_20();
//	t11_22();

	std::cout << "---------------------删除元素----------------------" << std::endl;
	t11_31();

	std::cout << "---------------------单词转换----------------------" << std::endl;
	t11_33();
	std::cout << std::endl;


	return 0;
}



